package com.powerhua.core.web;

import com.powerhua.core.bean.BeansRegisterLocator;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.RequestDispatcher;

public class PageLoaderServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 6370690589166720096L;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        StringBuffer result = new StringBuffer("");
        String servletAction = request.getParameter("s");
        try {
            if (servletAction != null && !"".equals(servletAction)) {
                IServletAction action = (IServletAction) 
                        BeansRegisterLocator.getBean(servletAction);

                // action result
                result.append(action.doAction(request));
            }

            // dispatcher
            // forward
            Object objForward = request.getAttribute("forward");
            if (objForward != null && (Boolean) objForward) {
                RequestDispatcher requestDispacher =
                        request.getRequestDispatcher(result.toString());
                requestDispacher.forward(request, response);
                return;
            }

            // redirect
            Object objRedirect = request.getAttribute("redirect");
            if (objRedirect != null && (Boolean) objRedirect) {
                response.sendRedirect(result.toString());
                return;
            }

            // write directly
            out.print(result.toString());

        } finally {
            out.close();
        }
    }

    /**
     * 
     */
    @Override
    public void init() throws ServletException {
        super.init();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
